package edu.chl.grupp11.aegas.model.imp;

import java.util.ArrayList;

import org.apache.sanselan.ImageWriteException;
import org.apache.sanselan.formats.tiff.TiffField;
import org.apache.sanselan.formats.tiff.constants.TiffFieldTypeConstants;
import org.apache.sanselan.formats.tiff.fieldtypes.FieldType;
import org.apache.sanselan.formats.tiff.write.TiffOutputField;

public class MutableJpgMetaTag extends MutableMetaTag {
	private static String removeQuoteChars(String string) {
		return string.replaceAll("'", "");
	}

	private TiffField dataField;

	public MutableJpgMetaTag(TiffField dataField) {
		super(dataField.tagInfo.name, removeQuoteChars(dataField
				.getValueDescription()));
		this.dataField = dataField;
	}

	public boolean arrayOnlyContains(FieldType specifiedType) {
		for (FieldType f : getFieldTypes()) {
			if (f.type != specifiedType.type)
				return false;
		}
		return true;
	}

	public TiffOutputField createTiffOutputField(String value)
			throws ImageWriteException {

		TiffOutputField tiffField = new TiffOutputField(getField().tagInfo,
				TiffFieldTypeConstants.FIELD_TYPE_ASCII, value.length(),
				value.getBytes());

		return tiffField;

	}

	public TiffField getField() {
		return dataField;
	}

	private ArrayList<FieldType> getFieldTypes() {
		FieldType[] temp = dataField.tagInfo.dataTypes;
		ArrayList<FieldType> fieldTypes = new ArrayList<FieldType>();
		for (FieldType f : temp) {
			fieldTypes.add(f);
		}

		return fieldTypes;
	}

}
